package com.sprouting.personal.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.personal.annotations.ApiIdempotent;
import com.sprouting.personal.interfaces.common.Constant;
import com.sprouting.personal.model.common.ResultJson;
import com.sprouting.personal.model.common.jsr.AddGroup;
import com.sprouting.personal.model.common.jsr.UpdGroup;
import com.sprouting.personal.model.entity.Config;
import com.sprouting.personal.model.entity.PsEvent;
import com.sprouting.personal.model.vo.PsEventVo;
import com.sprouting.personal.service.ConfigService;
import com.sprouting.personal.service.PsEventService;
import com.sprouting.personal.utils.CheckTool;
import com.sprouting.personal.utils.EntityTool;
import com.sprouting.personal.utils.IdTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 事件
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2022/11/16 15:51
 */
@RequestMapping("/event")
@RestController
public class PsEventController {

    @Autowired
    private PsEventService psEventService;
    @Autowired
    private ConfigService configService;


    /**
     * 根据条件查询事件
     * @param page 分页参数
     * @param psEvent 查询参数
     * @return
     */
    @PostMapping("/getEventListPage")
    public ResultJson getEventListPage(Page page, PsEventVo psEvent){
        if (psEvent == null){
            psEvent = new PsEventVo();
        }
        psEvent.setUserId(StpUtil.getLoginIdAsString());
        Page<PsEventVo> eventListPage = psEventService.getEventListPage(page, psEvent);
        List<PsEventVo> records = eventListPage.getRecords();
        if (CollUtil.isNotEmpty(records)){
            for (PsEventVo eventVo : records){
                if (Constant.EventLevel.CORE.equals(eventVo.getEventLevel())){
                    eventVo.setColor("#F56C6C");
                    eventVo.setIcon("");
                    eventVo.setGrade("特别重大事件");
                } else if (Constant.EventLevel.LINCHPIN.equals(eventVo.getEventLevel())){
                    eventVo.setColor("#E6A23C");
                    eventVo.setIcon("");
                    eventVo.setGrade("重大事件");
                } else if (Constant.EventLevel.MAJOR.equals(eventVo.getEventLevel())){
                    eventVo.setColor("#67C23A");
                    eventVo.setIcon("");
                    eventVo.setGrade("较大事件");
                } else {
                    eventVo.setColor("");
                    eventVo.setIcon("");
                    eventVo.setGrade("一般事件");
                }
            }
        }
        return ResultJson.success(eventListPage);
    }

    /**
     * 获取事件地点的地址
     * @return
     */
    @PostMapping("/getEventLocale")
    @ApiIdempotent
    public ResultJson<Config> getEventLocale(){
        Config config = configService.getConfigByCode(Constant.ConfigCode.EVENT_ADDRESS, StpUtil.getLoginIdAsString());
        if (config == null){
            // 构建当前用户自己的阅读地址，直接复制默认的
            config = configService.getConfigByCode(Constant.ConfigCode.EVENT_ADDRESS, Constant.DefaultValue.STRING);
            config.setId(IdTool.getId());
            config.setCode(Constant.ConfigCode.EVENT_ADDRESS);
            config.setUserId(StpUtil.getLoginIdAsString());
            configService.save(config);
        }
        return ResultJson.success(config);
    }

    /**
     * 保存事件
     * @param psEvent 事件
     * @return
     */
    @PostMapping("/savePsEvent")
    @ApiIdempotent
    public ResultJson savePsEvent(@Validated(AddGroup.class) PsEvent psEvent){
        String msg = checkPsEvent(psEvent);
        if (msg != null){
            return ResultJson.error(msg);
        }

        psEvent.setId(IdTool.getId());
        psEvent.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.buildSaveBase(psEvent, StpUtil.getLoginIdAsString());
        psEventService.save(psEvent);
        return ResultJson.success(psEvent);
    }

    /**
     * 修改事件
     * @param psEvent 事件
     * @return
     */
    @PostMapping("/updPsEvent")
    @ApiIdempotent
    public ResultJson updPsEvent(@Validated(UpdGroup.class) PsEvent psEvent){
        String msg = checkPsEvent(psEvent);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.buildUpdBase(psEvent, StpUtil.getLoginIdAsString());
        psEventService.updateById(psEvent);
        return ResultJson.success(psEvent);
    }

    /**
     * 根据主键删除事件
     * @param id 事件主键
     * @return
     */
    @PostMapping("/delPsEventById")
    public ResultJson delPsEventById(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorIllegal();
        }

        psEventService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 校验数据并补充数据
     * @param psEvent 事件
     * @return 非null则返回错误数据
     */
    private String checkPsEvent(PsEvent psEvent){
        if (psEvent.getEventLevel() == null){
            psEvent.setEventLevel(Constant.EventLevel.COMMONLY);
        }

        return null;
    }
}
